package com.kang.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.kang.dao.InventoryLiquidationMapper;
import com.kang.dao.ItemDetailMapper;
import com.kang.pojo.InventoryLiquidation;
import com.kang.pojo.InventoryLiquidationStatistics;
import com.kang.pojo.InventoryLiquidation_itemInformation;
import com.kang.pojo.ItemDetail;
import com.kang.service.InventoryLiquidationService;
import com.vonechina.entity.page.ItemLossPage;
import com.vonechina.entity.page.LiquidationListPage;
import com.vonechina.entity.page.LiquidationStatisticsListPage;

/**
 * 库存盘点--服务层接口实现类
 * @author wd
 *
 */
@Service
public class InventoryLiquidationServiceImpl implements InventoryLiquidationService {

	/**
	 * 库存盘点-DAO层
	 */
	@Resource
	private InventoryLiquidationMapper inventoryLiquidationMapper;
	/**
	 * 物品批次-DAO层
	 */
	@Resource
	private ItemDetailMapper idm;

	/**
	 * 查询盘点列表总条数
	 */
	@Override
	public int selectByWhereCount(LiquidationListPage liquidationListPage) {
		return inventoryLiquidationMapper.selectByWhereCount(liquidationListPage);
	}

	/**
	 * 查询盘点列表
	 */
	@Override
	public List<InventoryLiquidation> selectByWhereList(LiquidationListPage liquidationListPage) {
		return inventoryLiquidationMapper.selectByWhereList(liquidationListPage);
	}

	/**
	 * 根据库存盘点id删除库存盘点
	 */
	@Override
	public boolean deleteInventoryLiquidationById(int inventoryID) {
		int sql = inventoryLiquidationMapper.deleteInventoryLiquidationById(inventoryID);
		if(sql > 0){
			inventoryLiquidationMapper.deleteInventoryLiquidationToItemById(inventoryID);
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 根据盘点id查询盘点信息
	 */
	@Override
	public InventoryLiquidation getInventoryLiquidationById(Integer inventoryID) {
		return inventoryLiquidationMapper.getInventoryLiquidationById(inventoryID);
	}

	/**
	 * 根据盘点id查询盘点详情物品列表
	 */
	@Override
	public List<InventoryLiquidation_itemInformation> listItemByInventoryID(Integer inventoryID) {
		return inventoryLiquidationMapper.listItemByInventoryID(inventoryID);
	}

	/**
	 * 添加盘点品时查询物品列表
	 */
	@Override
	public List<ItemDetail> listItemDetailForLoss(ItemLossPage page) {
		page.setRows(idm.countItemNotInForLoss(page));
		return idm.listItemNotInForLoss(page);
	}

	/**
	 * 修改盘点单信息
	 */
	@Override
	public int updateInventoryLiquidation(InventoryLiquidation inventoryLiquidation) {
		return inventoryLiquidationMapper.updateInventoryLiquidation(inventoryLiquidation);
	}

	/**
	 * 删除盘点单的中不属于当前集合的物品id的物品
	 */
	@Override
	public int deleteItemNotInLiquidationSingle(Integer inventoryID,Integer[] itemIds) {
		return inventoryLiquidationMapper.deleteItemNotInLiquidationSingle(inventoryID,itemIds);
	}

	/**
	 * 保存盘点单中的物品
	 */
	@Override
	public int saveLiquidationItemList(Integer inventoryID, Integer[] itemIds,
			Integer[] number, Integer[] itemInformationIDs,Integer[] recordNumberS, Integer warehouseID) {
		if(itemIds==null){
			return 0;
		}
		for (int i = 0; i < itemIds.length; i++) {
			InventoryLiquidation_itemInformation pandianWupin = inventoryLiquidationMapper.selectLiquidationItemInfoByItemid(inventoryID,itemIds[i]);
			if(pandianWupin==null){
				pandianWupin = new InventoryLiquidation_itemInformation();
				pandianWupin.setInventoryID(inventoryID);
				pandianWupin.setWarehouseID(warehouseID);
				pandianWupin.setItemInformationID(itemInformationIDs[i]);
				pandianWupin.setRecordNumber(recordNumberS[i]);
				pandianWupin.setInventoryInventoryNumber(number[i]);
				pandianWupin.setSourceID(itemIds[i]);
				inventoryLiquidationMapper.insertNewItemToLiquidation(pandianWupin);
			}else{
				pandianWupin.setInventoryInventoryNumber(number[i]);
				pandianWupin.setProfitLossNumber(number[i]-recordNumberS[i]);
				inventoryLiquidationMapper.updateItemNumberToLiquidation(pandianWupin);
			}
		}
		
		return 1;
	}

	/**
	 * 提交盘点单
	 */
	@Override
	public int saveAndSubmitinventoryLiquidation(Integer userID,Integer inventoryID) {
		return inventoryLiquidationMapper.saveAndSubmitinventoryLiquidation(userID,inventoryID);
	}

	/**
	 * 盘点提交后修改实际库存数量
	 */
	@Override
	public int submitUpdateLiquidationAfterNumberForStock(Integer[] itemIds,Integer[] number) {
		for (int i = 0; i < itemIds.length; i++) {
			idm.updateBatchInventoryNumber(itemIds[i],number[i]);
		}
		return itemIds.length;
	}

	/**
	 * 生成盘点单号
	 */
	@Override
	public String selectMaxLiquidationNumber() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String text = sdf.format(new Date()) + "0001";
		Long no = Long.parseLong(text);
		String max = inventoryLiquidationMapper.selectMaxLiquidationNumber();
		Long l = 0l;
		if (max != null) {
			l = Long.parseLong(max.replaceAll("[a-zA-Z]", ""));
		}
		if (l >= no) {
			no = l + 1;
		}
		return "PD" + no;
	}

	/**
	 * 新增盘点单信息
	 */
	@Override
	public int addNewInventoryLiquidation(InventoryLiquidation inventoryLiquidation) {
		return inventoryLiquidationMapper.addNewInventoryLiquidation(inventoryLiquidation);
	}

	/**
	 * 根据盘点单号查询盘点id
	 */
	@Override
	public Integer getIdByInventoryNumber(String inventoryNumber) {
		return inventoryLiquidationMapper.getIdByInventoryNumber(inventoryNumber);
	}

	/**
	 * 盘点统计
	 */
	@Override
	public List<InventoryLiquidationStatistics> selectInventoryLiquidationStatisticsList(
			LiquidationStatisticsListPage liquidationStatisticsListPage) {
		liquidationStatisticsListPage.setRows(inventoryLiquidationMapper.selectInventoryLiquidationStatisticsCount(liquidationStatisticsListPage));
		return inventoryLiquidationMapper.selectInventoryLiquidationStatisticsList(liquidationStatisticsListPage);
	}
	
	
}
